package com.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mall.common.page.PageData;
import com.mall.common.service.impl.CrudServiceImpl;
import com.mall.common.utils.ConvertUtils;
import com.mall.common.utils.ParamUtils;
import com.mall.ware.dao.PurchaseDetailDao;
import com.mall.ware.dto.PurchaseDetailDTO;
import com.mall.ware.entity.PurchaseDetailEntity;
import com.mall.ware.service.PurchaseDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@Service
public class PurchaseDetailServiceImpl extends CrudServiceImpl<PurchaseDetailDao, PurchaseDetailEntity, PurchaseDetailDTO> implements PurchaseDetailService {

    @Override
    public QueryWrapper<PurchaseDetailEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public PageData<PurchaseDetailDTO> queryPageByCondition(Map<String, Object> params) {
        String key = ParamUtils.strParseString(params.get("key"));
        Long wareId = ParamUtils.strParseLong(params.get("wareId"));
        String status = ParamUtils.strParseString(params.get("status"));
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key))
            wrapper.and(q->q.eq("id",key).or().eq("purchase_id",key).or().eq("sku_id",key));
        wrapper.eq(wareId>0,"ware_id",wareId);
        wrapper.eq(StringUtils.isNotBlank(status),"status",status);
        IPage<PurchaseDetailEntity> selectPage = baseDao.selectPage(getPage(params, null, false), wrapper);
        return new PageData<>(ConvertUtils.sourceToTarget(selectPage.getRecords(),PurchaseDetailDTO.class),selectPage.getTotal());
    }

    @Override
    public List<PurchaseDetailEntity> listByPurchaseId(Long purchaseId) {
        List<PurchaseDetailEntity> selectList = baseDao.selectList(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", purchaseId));
        return selectList;
    }

}